package com.yy.job.common.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Preconditions;
import com.yy.job.common.dao.YYJobDOMapper;
import com.yy.job.common.dao.po.YYJobDO;
import com.yy.job.common.dao.po.YYJobDOExample;
import com.yy.job.common.dao.po.YYJobDOExample.Criteria;
import com.yy.job.common.dao.po.YYJobStepDO;
import com.yy.job.common.service.IBaseService;

@Service
public class YYJobServiceImpl implements IBaseService<YYJobDO> {

	@Autowired
	private YYJobDOMapper yyJobDOMapper;
	
	@Autowired
	private YYJobStepServiceImpl jobStepServiceImpl;

	@Override
	public Long saveSelective(YYJobDO jobDO) {
		Date nowDate = new Date();
		if (jobDO.getId() == null) {
			if (jobDO.getGmtCreated() == null) {
				jobDO.setGmtCreated(nowDate);
			}
			if (jobDO.getGmtModified() == null) {
				jobDO.setGmtModified(nowDate);
			}
			if (StringUtils.isEmpty(jobDO.getIsDeleted())) {
				jobDO.setIsDeleted("N");
			}
			this.yyJobDOMapper.insert(jobDO);
			return jobDO.getId();
		} else {
			if (jobDO.getGmtModified() == null) {
				jobDO.setGmtModified(nowDate);
			}
			this.yyJobDOMapper.updateByPrimaryKeySelective(jobDO);
			return jobDO.getId();
		}
	}
	
	/**
	 * 级联保存两级数据
	 * 
	 * @param jobDO
	 * @param taskDOList
	 * @return
	 */
	public Long saveCascade(YYJobDO jobDO, List<YYJobStepDO> taskDOList) {
		Long jobId = this.saveSelective(jobDO);
		if (CollectionUtils.isNotEmpty(taskDOList)) {
			for (YYJobStepDO taskDO : taskDOList) {
				taskDO.setJobId(jobId);
				this.jobStepServiceImpl.saveSelective(taskDO);
			}
		}
		return jobId;
	}

	/**
	 * 根据projectCode + moduleCode + jobCode + businessNo查询
	 * 
	 * @param moduleCode
	 * @param businessNo
	 * @param jobCode
	 * @return
	 */
	public YYJobDO selectJobByBusinessNo(
			@Param("projectCode") String projectCode,
			@Param("moduleCode") String moduleCode, 
			@Param("jobCode") String jobCode,
			@Param("businessNo") String businessNo) {
		List<YYJobDO> selectJobListByBusinessNo = this.selectJobListByBusinessNo(projectCode, moduleCode, jobCode, businessNo);
		if(CollectionUtils.isEmpty(selectJobListByBusinessNo)) {
			return null;
		}
		return selectJobListByBusinessNo.get(0);
	}
	
	public List<YYJobDO> selectJobListByBusinessNo(
			@Param("projectCode") String projectCode,
			@Param("moduleCode") String moduleCode, 
			@Param("jobCode") String jobCode,
			@Param("businessNo") String businessNo) {
		Preconditions.checkArgument(StringUtils.isNotBlank(projectCode), "projectCode is null");
		Preconditions.checkArgument(StringUtils.isNotBlank(moduleCode), "moduleCode is null");
		Preconditions.checkArgument(StringUtils.isNotBlank(jobCode), "jobCode is null");
		Preconditions.checkArgument(StringUtils.isNotBlank(businessNo), "businessNo is null");
		
		YYJobDOExample example = new YYJobDOExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andProjectCodeEqualTo(projectCode).andModuleCodeEqualTo(moduleCode).andJobCodeEqualTo(jobCode).andBusinessNoEqualTo(businessNo)
				.andIsDeletedEqualTo("N");

		List<YYJobDO> selectByExample = yyJobDOMapper.selectByExample(example);
		return selectByExample;
	}
	
}
